package Articulo.src.view;

import java.rmi.RemoteException;

import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.StatusLineManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import Articulo.src.business.interfaces.ArticleMgt;
import Articulo.src.business.managers.ArticleMgr;

import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.custom.TableCursor;

import Articulo.src.business.entities.*;
import Articulo.src.business.managers.*;;


public class verProductos extends ApplicationWindow {
	private Table table;
	 Article[] art;

	/**
	 * Create the application window.
	 */
	public verProductos() {
		super(null);
		this.setStatus("Productos");
		
		
		createActions();
		addToolBar(SWT.FLAT | SWT.WRAP);
		addMenuBar();
		addStatusLine();
	}

	/**
	 * Create contents of the application window.
	 * @param parent
	 */
	@Override
	protected Control createContents(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		
		Composite composite = new Composite(container, SWT.NONE);
		composite.setBounds(24, 47, 416, 173);
		
		table = new Table(composite, SWT.BORDER | SWT.FULL_SELECTION);
		
		table.setBounds(0, 0, 406, 173);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tblclmnNom= new TableColumn(table, SWT.NONE);
		tblclmnNom.setWidth(100);
		tblclmnNom.setText("Nombre");
		
		TableColumn tblclmnprecio = new TableColumn(table, SWT.NONE);
		tblclmnprecio.setWidth(49);
		tblclmnprecio.setText("Precio");
		
		TableColumn tblclmnCat = new TableColumn(table, SWT.NONE);
		tblclmnCat.setWidth(100);
		tblclmnCat.setText("New Column");
		
		TableColumn tblclmnDes = new TableColumn(table, SWT.NONE);
		tblclmnDes.setWidth(100);
		tblclmnDes.setText("New Column");
		
		TableCursor tableCursor = new TableCursor(table, SWT.NONE);
		ArticleMgr mg = new ArticleMgr();
		try {
			this.art = mg.getArticles();
		} catch (RemoteException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		//TableItem [] items = new TableItem[art.length];
		for(int i= 0; i<art.length;i++){
			//TableItem tableItem = new TableItem(table, SWT.NONE);
			//tableItem.setText("New TableItem");
			//Pedirle a nacho que me pase el numer de atributos
			String [] info = new String[4];
			info[0]= art[i].getName();
			info[1]= Integer.toString(art[i].getPrice());
			if(art[i].getCat()!=null){
			info[2]= art[i].getCat().toString();}
			else{
				info[2]="no";
			}
			info[3]=art[i].getDescr();
			
		
			
			TableItem tableItem = new TableItem(table, SWT.NONE);
			
			tableItem.setText(info);
			
			
		}
		Button btnVolver = new Button(container, SWT.NONE);
		btnVolver.addSelectionListener(new SelectionAdapter() {
		
			public void widgetSelected(SelectionEvent e) {
			close();	
			}
		});
		btnVolver.setBounds(281, 226, 94, 28);
		btnVolver.setText("VOLVER");
		
		
		

		return container;
	}

	/**
	 * Create the actions.
	 */
	private void createActions() {
		// Create the actions
	}

	/**
	 * Create the menu manager.
	 * @return the menu manager
	 */
	@Override
	protected MenuManager createMenuManager() {
		return null;
	}

	
	
	protected StatusLineManager createStatusLineManager() {
		StatusLineManager statusLineManager = new StatusLineManager();
		statusLineManager.setMessage("Productos Existentes");
		return statusLineManager;
	}

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			verProductos window = new verProductos();
			window.setBlockOnOpen(true);
			window.open();
			Display.getCurrent().dispose();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Configure the shell.
	 * @param newShell
	 */
	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("Productos");
	}

	/**
	 * Return the initial size of the window.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(450, 300);
	}
}
